#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include"MyString.h"
#include "DynamicArray.h"

#define true 1
#define false 0

#define Map_Size 5
struct Player
{
    int HP;
    int attack;
    int x;
    int y;
};

struct Monster
{
    MyString name;
    int HP;
    int attack;
    int x;
    int y;
};
struct Treasure
{
    MyString name;
    int value;
    int x;
    int y;
};

void InitPlayer(struct Player *player )
{
    player->HP=100;
    player->attack = 10;
    player->x = 0;
    player->y = 0;
}

int GetRandNumber(int max)
{
    return rand() % max;
}

struct Monster* CreateMonster(const char* name,int HP,int attack)
{
    struct Monster *monster =(struct Monster*)malloc(sizeof (struct Monster));
    if(monster == NULL)
    {
        printf("Create Monster Error!\n");
        return NULL;
    }
    monster->HP=HP;
    monster->attack=attack;
    Initialize(&monster->name,name);
    monster->x=GetRandNumber(Map_Size);
    monster->y=GetRandNumber(Map_Size);
    
    return monster;
}

struct Treasure* CreateTreasure(const char* name,int value)
{
    struct Treasure *Treasure =(struct Treasure*)malloc(sizeof (struct Treasure));
    if(Treasure == NULL)
    {
        printf("Create Treasure Error!\n");
        return NULL;
    }
 
    Initialize(&Treasure->name,name);
    Treasure->value=value;
    Treasure->x=GetRandNumber(Map_Size);
    Treasure->y=GetRandNumber(Map_Size);
    
    return Treasure;
}

void InitMonsters(struct DynamicArray *array)
{
   if(ArrayInit(array) == false)
   {
    return;
   }
   InsertArray(array,CreateMonster("巨龙",100,20));
   InsertArray(array,CreateMonster("食人魔",50,10));
   InsertArray(array,CreateMonster("食人魔",50,10));
   InsertArray(array,CreateMonster("食人魔",50,10));
   InsertArray(array,CreateMonster("哥布林",20,5));
   InsertArray(array,CreateMonster("哥布林",20,5));
   InsertArray(array,CreateMonster("哥布林",20,5));
   InsertArray(array,CreateMonster("哥布林",20,5));

}

void InitTreasures(struct DynamicArray *array)
{
    if(ArrayInit(array) == false)
   {
    return;
   }
   InsertArray(array,CreateTreasure("宝藏",100));
   InsertArray(array,CreateTreasure("珍珠",50));
   InsertArray(array,CreateTreasure("珍珠",50));
   InsertArray(array,CreateTreasure("珍珠",50));
   InsertArray(array,CreateTreasure("药品",20));
   InsertArray(array,CreateTreasure("药品",20));
   InsertArray(array,CreateTreasure("药品",20));
   InsertArray(array,CreateTreasure("药品",20));
   InsertArray(array,CreateTreasure("药品",20));
}
void MakeMove(struct Player *player,char symbol)
{
    switch (symbol)
    {
        case 'w':
            player->y--;
            break;
        case 's':
            player->y++;
            break; 
        case 'a':
            player->x--;
            break; 
        case 'd':
            player->x++;
            break;
        default:
            break; 
    }

    if(player->x < 0) player->x =0;
    if(player->x >= Map_Size) player->x =Map_Size-1;
    if(player->y < 0)player->y =0;
    if(player->y >= Map_Size) player->y =Map_Size-1;
}

int Battle(struct Player *player,struct Monster* monster)
{
    system("clear");
    printf("你遭遇了|%s| 血量:|%d|  攻击力:|%d|\n",monster->name.string,monster->HP,monster->attack);
    printf("*********************************\n");
    printf("*             开始战斗           *\n");
    printf("*********************************\n");
    while (player->HP >0 && monster > 0)
 {
     printf("请选择你要执行的行为：\n");
     printf("1.普通攻击：\n");  
     printf("2.暴击\n");
     printf("3.逃跑\n");
     int choice;
     scanf("%d",&choice);
     printf("===========================战斗报告===========================\n");
     switch (choice)
     {
        case 1:
             player->HP -= monster->attack;
             monster->HP -= player->attack;
             printf("|%s|对你造成了%d的伤害\n",monster->name.string,monster->attack);
             printf("你对|%s|造成了%d的伤害\n",monster->name.string,monster->attack);
             break;
        case 2:
             if(GetRandNumber(2) == 1)
             {
                 printf("|%s|对你造成了%d的伤害\n",monster->name.string,monster->attack);
                 printf("你对|%s|造成了%d的伤害\n",monster->name.string,monster->attack*2);
                player->HP -=monster->attack;
                monster->HP -=player->attack * 2;
             }
             else 
             {
                printf("|%s|对你造成了%d的伤害\n",monster->name.string,monster->attack);
                printf("暴击失败！无法发起攻击！\n");
                player->HP -=monster->attack;
             }
             break;
             case 3:
             if(GetRandNumber(2) == 1)
             {
                 printf("逃跑成功");
             } 
             else 
             {
                printf("逃跑失败\n");
                printf("|%s|对你造成了%d的伤害\n",monster->name.string,monster->attack);
                player->HP -=monster->attack;
             }
             default:
             break;       
    }
     printf("===========================end===========================\n");

      if(player->HP <= 0)
     {
        printf("你被 |%s| 击败了!\n",monster->name.string);
        player->HP = 0;
        return false;
     }
     if(monster->HP <= 0)
     {
        monster->HP = 0;
        printf("你击败了 |%s|!\n",monster->name.string);
        return true;
     }

  }
}

void InitBoard(char (*p)[Map_Size])
{
  for (int i = 0; i < Map_Size; i++)
  {
    for (int j = 0;j < Map_Size; j++)
    {
        p[i][j] = '-';
    }
  }
}

void PrintMap(char (*p)[Map_Size],struct Player *player)
{
   InitBoard(p);
   p[player->y][player->x] = 'x';
     printf("       ");
    for (int i = 0 ; i < Map_Size; i++)
    {
        printf("  %4d ",i+1);
    }
    printf("\n");
    for (int i = 0; i < Map_Size; i++)
    {
        printf("  %4d ",i+1);
        for (int j = 0; j < Map_Size; j++)
        {
            printf("  %4c ",p[i][j]); 
        }
        printf("\n");
    }
}

int main()
{
    srand (time(NULL));

    printf("*******************************************\n");
    printf("*            欢迎来到魔塔世界             *\n");
    printf("*******************************************\n");

    char board[Map_Size][Map_Size];
    InitBoard(board);

    struct  Player player ;
    InitPlayer(&player);

    struct DynamicArray monsters ={NULL,10,0};
    InitMonsters(&monsters);
    
    struct DynamicArray treasures = {NULL,10,0};
    InitTreasures(&treasures);

    PrintMap(board,&player);
    
    while (1)
    {
      
      PrintMap(board,&player);
      printf("你当前所在位置 <%d,%d>\n", player.y+1, player.x+1);

      for (int i = 0; i < monsters.len; i++)
      {
        struct Monster *monster = (struct Monster*)monsters.dp[i];     
        if(monster->HP >0 && monster->x == player.x && monster->y == player.y)
        {
            if(Battle(&player,monster) == false)
            {
                printf("Game over!");
                break;
            } 
        }
      }
      if(player.HP == 0)
       break;
      printf("请选择你要进行的运动(输入'w','a','s','d'):");
      printf("\n");
      char choice;
      scanf(" %c",&choice);
      MakeMove(&player,choice);
    }
    for(int i = 0;i < monsters.len; i++)
    {
      struct Monster *monster = (struct Monster*)monsters.dp[i];
      free(monster->name.string);
      free(monsters.dp);
    }
    return 0;
}
